package com.server.http.service.imlp;

import com.server.dao.MobileAppDao;
import com.server.pojo.MobileApp;
import com.server.libs.login.AppAuth;
import com.server.libs.login.data.AppCache;
import com.server.libs.ret.RE;
import com.server.http.service.MobileAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@Transactional
public class MobileAppServiceImpl implements MobileAppService {
    @Autowired
    AppAuth appAuth;

    @Autowired
    MobileAppDao mobileAppDao;

    @Override
    public MobileApp checkLogin(String auth) {
        MobileApp mobileApp = this.get(auth);
        if (mobileApp == null) throw new RE("你没有登录");
        return mobileApp;
    }

    @Override
    public MobileApp get(String auth) {
        AppCache cache = this.getCache(auth);
        if (cache == null)return null;
        Optional<MobileApp> byId = mobileAppDao.findById(cache.id);
        return byId.orElse(null);
    }

    @Override
    public MobileApp get(Integer id) {
        Optional<MobileApp> byId = mobileAppDao.findById(id);
        return byId.orElse(null);
    }
    @Override
    public AppCache getCache(String auth){
        return appAuth.get(auth);
    }
    @Override
    public AppCache getCache(Integer id){
        return appAuth.get(id);
    }
}
